package kr.co.sdsway2.service;

import java.util.List;

import javax.annotation.Resource;

import kr.co.sdsway2.comm.SdsWay2Util;
import kr.co.sdsway2.common.AbstractService;
import kr.co.sdsway2.vo.PollInfVO;

import org.springframework.stereotype.Service;

@Service
public class PollInfBeanService extends AbstractService {

	@Resource(name="pollInfBeanList")
	private List<PollInfVO> pollInfBeanList;

	/** 새로운 POLL 입력 */
	public int insertPollInfBean(Integer ptId,Integer pollId,String qstTypeCd,String qstVal) {
		return insertPollInfBean(ptId,pollId,qstTypeCd,qstVal,"","","","",0,0,0,0);
	}
	
	/** 새로운 POLL 입력 */
	public int insertPollInfBean(Integer ptId,Integer pollId,String qstTypeCd,String qstVal
			,String opt1,String opt2,String opt3,String opt4
			,Integer optPoint1,Integer optPoint2,Integer optPoint3,Integer optPoint4) {
		if(getIndex(ptId,pollId)<0){ //없을때만 insert
			PollInfVO vo = new PollInfVO();
			vo.setPtId(ptId);
			vo.setPollId(pollId);
			vo.setQstTypeCd(qstTypeCd);
			vo.setQstVal(qstVal);
			vo.setOpt1(opt1);
			vo.setOpt2(opt2);
			vo.setOpt3(opt3);
			vo.setOpt4(opt4);
			vo.setOptPoint1(optPoint1);
			vo.setOptPoint2(optPoint2);
			vo.setOptPoint3(optPoint3);
			vo.setOptPoint4(optPoint4);
			
			pollInfBeanList.add(vo);
			return 1;	//성공
		}else{
			return 0;	//실패
		}
	}

	/** POLL 삭제(by ID) 서비스 */
	public void remove(Integer ptId,Integer pollId) {
		int idx = getIndex(ptId,pollId);
		if( idx != -1 ) this.pollInfBeanList.remove(idx);
	}

	/** POLL 삭제(by index) 서비스 */
	public void remove(int idx){
		this.pollInfBeanList.remove(idx);
	}

	/** POLL 목록 조회 서비스 */
	public List<PollInfVO> getList(){
		return this.pollInfBeanList;
	}

	/** POLL 조회 서비스 */
	public PollInfVO getPollInfBean(Integer ptId,Integer pollId) {
		int i = getIndex(ptId,pollId);
		if(i>=0){
			return pollInfBeanList.get(i);
		}
		return null;
	}

	/** POLL 상태 변경 서비스 */
	public void update(Integer ptId,Integer pollId,String qstTypeCd,String qstVal){
		update(ptId,pollId,qstTypeCd,qstVal,"","","","",0,0,0,0);
	}
	
	/** POLL 상태 변경 서비스 */
	public void update(Integer ptId,Integer pollId,String qstTypeCd,String qstVal
			,String opt1,String opt2, String opt3, String opt4
			,Integer optPoint1,Integer optPoint2,Integer optPoint3,Integer optPoint4) {
		PollInfVO vo = null;
		int i = getIndex(ptId,pollId);
		if(i<0){
			insertPollInfBean(ptId, pollId,qstTypeCd,qstVal,opt1,opt2,opt3,opt4,optPoint1,optPoint2,optPoint3,optPoint4);
			i = getIndex(ptId,pollId);
		}
		if(i>=0){
			vo = pollInfBeanList.get(i);
			vo.setQstTypeCd(qstTypeCd);
			vo.setQstVal(qstVal);
			if(!SdsWay2Util.isNull(opt1)) vo.setOpt1(opt1);
			if(!SdsWay2Util.isNull(opt2)) vo.setOpt2(opt2);
			if(!SdsWay2Util.isNull(opt3)) vo.setOpt3(opt3);
			if(!SdsWay2Util.isNull(opt4)) vo.setOpt4(opt4);
			vo.setOptPoint1(optPoint1);
			vo.setOptPoint2(optPoint2);
			vo.setOptPoint3(optPoint3);
			vo.setOptPoint4(optPoint4);
		}
	}

	/** POLL ID 통해 index 획득 */
	private int getIndex(Integer ptId,Integer pollId){
		int idx = -1;
		int size = pollInfBeanList.size();
		PollInfVO vo;
		for(int i=0;i<size;i++){
			vo = pollInfBeanList.get(i);
			if(vo!=null && vo.getPtId()!=null && vo.getPollId()!= null){
				if(vo.getPtId().equals(ptId) && vo.getPollId().equals(pollId))
					idx = i;
			}
		}
		return idx;
	}
	
}
